package com.huika.renrench.control.welcome;

import java.io.File;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.view.View;
import android.widget.ImageView;

import com.huika.lib.utils.DebugTool;
import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.main.activity.MainActivity;

public class WelcomeActivity extends BaseAct implements AnimatorListener {
	private ImageView iv;

	
	@Override
	public void setRootView() {
		setContentView(R.layout.activity_welcome);
		iv = (ImageView) findViewById(R.id.iv);
		startAnim();
	}
	
	
	/**
	 * 下一步跳转
	 * 
	 * @author FAN 创建于Dec 1, 2014
	 */
	private void forwardStep() {
//		new WebNetUtils(this).webNet();
		if (firstsInstall()) {// 首次安装
			DebugTool.i("zwy", "首次安装");
			skipActivity(this, GuideViewAct.class);
		} else {
			DebugTool.i("zwy", "不是首次安装");
			skipActivity(this, MainActivity.class);
		}
//		skipActivity(WelcomeActivity.this, LoginActivity.class);
	}


	private boolean firstsInstall() {
		File files = getFilesDir();
		File installFile = new File(files, "install");

		int newVC = 0;
		try {
			newVC = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
		} catch (Exception e) {
		}

		boolean firstInstall = installFile.exists();
		if (!firstInstall) {// 文件夹不存在，则表示初次安装
			installFile.mkdirs();
			try {
				new File(installFile, newVC + "").createNewFile();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return true;
		} else {
			String[] fs = installFile.list();
			if (fs == null || fs.length == 0) {// 上一个版本为空
				try {
					new File(installFile, newVC + "").createNewFile();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return true;
			}
			String lastV = fs[0];
			if (newVC > Integer.parseInt(lastV)) {
				try {
					new File(installFile, newVC + "").createNewFile();
					for (String vf : fs) {
						File temp = new File(installFile, vf);
						if (temp.exists())
							temp.delete();
					}
					return true;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}
	
	
	private void startAnim() {
		PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 0.2f, 1f);
		PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 0.2f, 1.5f);
		PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 0.2f, 1.5f);
		PropertyValuesHolder ry = PropertyValuesHolder.ofFloat("rotationY", 0f, 360f, 0.f);
		ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(iv, pvhX, pvhY, pvhZ, ry).setDuration(2500);
		anim.addListener(this);
		anim.start();
	}

	@Override
	public void onAnimationStart(Animator animation) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAnimationEnd(Animator animation) {
		long du = animation.getDuration();
		if(du == 2500){
			findViewById(R.id.tv).setVisibility(View.VISIBLE);
			
			PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1.5f, 1.0f);
			PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1.5f, 1.0f);
			ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(iv, pvhY, pvhZ).setDuration(1500);
			anim.addListener(this);
			anim.start();
		}else{
//			Intent indexIntent = new Intent(WelcomeActivity.this, IndexActivity.class);
//			startActivity(indexIntent);
//			finish();
			forwardStep();
		}
	}

	@Override
	public void onAnimationCancel(Animator animation) {
		
	}

	@Override
	public void onAnimationRepeat(Animator animation) {
		
	}

}
